package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.PengYouQuanInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.RoundImageView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 沐诺 on 2017/4/8.
 */

public class SchoolReportAdapter extends BaseAdapter {
    private int mMark = 0;
    private Context mContext;
    private List<PengYouQuanInfo> mList = new ArrayList<>();
    private String s;

    public SchoolReportAdapter(Context mContext, int mark) {
        this.mContext = mContext;
        mMark = mark;
    }

    public void updateData(List<PengYouQuanInfo> list, boolean isclear) {
        if (mList != null && isclear) {
//            mList.clear();
            mList.removeAll(mList);
        }
        mList.addAll(list);
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public PengYouQuanInfo getItem(int position) {
        PengYouQuanInfo info;
        if (mMark == 1) {
            info = mList.get(position);
        } else {
            info = mList.get(position - 1);
        }
        return info;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_schoolreport, null);
            viewHolder.title = (TextView) convertView.findViewById(R.id.item_schoolreport_title);
            viewHolder.time = (TextView) convertView.findViewById(R.id.item_schoolreport_time);
            viewHolder.browse = (TextView) convertView.findViewById(R.id.item_schoolreport_browse);
            viewHolder.laud = (TextView) convertView.findViewById(R.id.item_schoolreport_laud);
            viewHolder.image = (RoundImageView) convertView.findViewById(R.id.item_schoolreport_image);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        PengYouQuanInfo pengYouQuanInfo = mList.get(position);
        if (pengYouQuanInfo.getDairyContent() != null) {
            try {

                String title_text = URLDecoder.decode(pengYouQuanInfo.getDairyTitle(), "utf-8");
                if (title_text.length() > 20) {
                    viewHolder.title.setText(title_text.substring(0, 20) + "...");
                } else {
                    viewHolder.title.setText(title_text);
                }

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        viewHolder.time.setText(pengYouQuanInfo.getDairyTime());
        viewHolder.browse.setText(pengYouQuanInfo.getViewCount());
        viewHolder.laud.setText(pengYouQuanInfo.getUpCount());
        String anchorImgUrl = pengYouQuanInfo.getDairyImage();
        if (anchorImgUrl != null && anchorImgUrl.length() > 0) {
            ViewUtil.Size size = ViewUtil.getHeadImageSize_40(mContext);
            Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(anchorImgUrl, size));
            if (cacheBitmap != null) {
                viewHolder.image.setImageBitmap(cacheBitmap);
            } else {
                ImageCache.getInstance(mContext).loadBitmapToView(viewHolder.image, anchorImgUrl, size);
            }
        }
        return convertView;
    }

    class ViewHolder {
        TextView title;
        TextView time;
        TextView browse;
        TextView laud;
        RoundImageView image;
    }
}
